const {
  createweather,
  getweatherInfo,
  findAlldata,
  updateweather,
} = require("../service/weather.service");

class weatherController {
  async create(ctx,next) {
    console.log(ctx.request.body);
    if (await getweatherInfo(ctx.request.body)) {
        const res = await updateweather(ctx.request.body);
        ctx.body = {
          code: 0,
          message: "更新天气数据成功",
          result: res,
        };
    }else{
        const res = await createweather(ctx.request.body);
        ctx.body = {
          code: 0,
          message: "创建天气数据成功",
          result: res,
        };
    }
  }



  async update(ctx,next) {
    try {
      if (await updateweather(ctx.request.body)) {
        ctx.body = {
          code: 200,
          message: "天气数据更新成功",
          result: "",
        };
      } else {
        ctx.body = {
          code: "10007",
          message: "用户暂未修改",
          result: "",
        };
      }
    } catch (error) {
      ctx.body = {
        code: "10007",
        message: "天气数据更新失败",
        result: error,
      };
    }
  }

   // 全部
   async findAll(ctx,next) {
    // 2. 调用数据处理的相关方法
    const res = await findAlldata()
    // 3. 返回结果
    ctx.body = {
      code: 0,
      message: '天气数据列表获取成功',
      result: res,
    }
  }
}

module.exports = new weatherController();
